<?php

/**
 * This page receives and handles data from the servers. If the server wasn't found
 * on the masterlist yet we add, otherwise we just update the last time the ping was
 * sent in the database.
 * 
 * @author Matthias Van Eeghem <matthias@van-eeghem.com>
 * @copyright Copyright (c) 2012, Matthias Van Eeghem
 */

 // We need these defines for the MySQL details
require_once('Defines.php');

// Make sure we actually received all $_GET parameters from the server
if(isset($_GET['version']) && isset($_GET['port']) && isset($_GET['players']) && isset($_GET['hostname']) && isset($_GET['security_key']))
{
	// Get the server info from the GET superglobal
	$sVersion = $_GET['version'];
	$sIP = $_SERVER['REMOTE_ADDR'] . ':' . $_GET['port'];
	$iOnlinePlayers = intval($_GET['players']);
	$sHostname = $_GET['hostname'];
	$sKey = $_GET['security_key'];
	
	// Make sure the security key is correct, so only actual servers can be submitted to the master list
	if($sKey == "some_key")
	{
		// Connect to the MySQL server
		$MySQLi = new mysqli(MySQL_Server, MySQL_User, MySQL_Password, MySQL_Database);

		// Get the current epoch time
		$iLastUpdate = time();

		// We either INSERT INTO the database or UPDATE the last ping 
		$preparedStatement = $MySQLi->prepare("INSERT INTO MasterList (IP, Version, Hostname, OnlinePlayers, LastPing) VALUES (?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE OnlinePlayers=?, LastPing=?, Hostname=?");
		$preparedStatement->bind_param("sssiiiis", $sIP, $sVersion, $sHostname, $iOnlinePlayers, $iLastUpdate, $iOnlinePlayers, $iLastUpdate, $sHostname);
		$preparedStatement->execute();
		$preparedStatement->close();
	}
	else
	{
		echo 'Invalid security key';
	}
}

?>